function reactive(target) {
    return createReactiveObject(target);
}

function isObject(obj) {
    return obj !== null && typeof obj === 'object';
}


function createReactiveObject(target) {
    // 如果不是对象，直接返回
    if (!isObject(target)) {
        return target;
    }

    let baseHandler = {
        get(target, key, receiver) {
            console.log('读取值');
            let result = Reflect.get(target, key, receiver);
            return isObject(result) ? reactive(result) : result;
        },
        set(target, key, value, receiver) {
            console.log('设置值');
            let result = Reflect.set(target, key, value, receiver);
            updateView();
            return result;
        }
    };
    let observer = new Proxy(target, baseHandler);
    return observer;
}

function updateView() {
    console.log(`更新视图`);
}

let data = {
    name: '阿伟',
    age: {
        n: 18
    },
    like: ['吃饭', '睡觉']
};
let newData = reactive(data);
// newData.name = '子俊';
// console.log(newData.name);
newData.age.n = 20;
console.log(newData.age.n);